﻿/**
 * Copyright (C) 2012-2013 original author and authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NGuice.Inject.Matcher
{
    /// <summary>
    /// 实现And和Or操作
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class AbstractMatcher<T> : IMatcher<T>
    {
        public abstract bool Match(T t);

        public bool Matches(object t)
        {
            return Matches(t);
        }

        public IMatcher<T> And(IMatcher other)
        {
            return new AndMatcher<T>(this, other);
        }

        [Serializable]
        private class AndMatcher<T> : AbstractMatcher<T>
        {
            private readonly IMatcher a;
            private readonly IMatcher b;

            public AndMatcher(IMatcher a, IMatcher b)
            {
                this.a = a;
                this.b = b;
            }

            public override bool Match(T t)
            {
                return a.Matches(t) && b.Matches(t);
            }

            public override bool Equals(object other)
            {
                return other is AndMatcher<T>
                    && ((AndMatcher<T>)other).a.Equals(a)
                    && ((AndMatcher<T>)other).b.Equals(b);
            }

            public override int GetHashCode()
            {
                return 41 * (a.GetHashCode() ^ b.GetHashCode());
            }

            public override string ToString()
            {
                return "and(" + a + ", " + b + ")";
            }
        }

        public IMatcher<T> Or(IMatcher other)
        {
            return new OrMatcher<T>(this, other);
        }

        [Serializable]
        private class OrMatcher<T> : AbstractMatcher<T>
        {
            private readonly IMatcher a;
            private readonly IMatcher b;

            public OrMatcher(IMatcher a, IMatcher b)
            {
                this.a = a;
                this.b = b;
            }

            public override bool Match(T t)
            {
                return a.Matches(t) || b.Matches(t);
            }

            public override bool Equals(object other)
            {
                return other is OrMatcher<T>
                    && ((OrMatcher<T>)other).a.Equals(a)
                    && ((OrMatcher<T>)other).b.Equals(b);
            }

            public override int GetHashCode()
            {
                return 37 * (a.GetHashCode() ^ b.GetHashCode());
            }

            public override string ToString()
            {
                return "or(" + a + ", " + b + ")";
            }
        }

    }
}
